package mes

import (
	"context"
	"encoding/json"
	"ez/apps/mes/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type Paper struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64 `json:"id" bson:"id,omitempty"`

	Plan string `json:"plan" bson:"plan"` //计划

	Sn string `json:"sn" bson:"sn"` //条码

	Flow *Flow `json:"flow" bson:"flow"` //跟踪单

	FlowId int64 `json:"flowId" bson:"flowId"` //跟踪单

	Process *Process `json:"process" bson:"process"` //工序

	ProcessId int64 `json:"processId" bson:"processId"` //工序

	NumSuccess float64 `json:"numSuccess" bson:"numSuccess"` //成品

	NumFail float64 `json:"numFail" bson:"numFail"` //修品

	NumWast float64 `json:"numWast" bson:"numWast"` //废品

	Emp *Emp `json:"emp" bson:"emp"` //员工

	EmpId int64 `json:"empId" bson:"empId"` //员工

	SrcPapers []*PaperLinks `json:"srcPapers" bson:"srcPapers"` //源单

	UsedPapers []*PaperLinks `json:"usedPapers" bson:"usedPapers"` //被使用

	NumUsed float64 `json:"numUsed" bson:"numUsed"` //已使用

	CreateAt time.Time  `json:"createAt" bson:"createAt"`
	UpdateAt time.Time  `json:"updateAt" bson:"updateAt"`
	DeleteAt *time.Time `json:"deleteAt" bson:"deleteAt"`
}

func (this *Paper) DocName() string { return "Paper" }

func (this *Paper) GetId() int64 { return this.Id }

func (this *Paper) SetId(id int64) { this.Id = id }

func (this *Paper) Create() error {
	return this.GetFactory().Create(this)
}

func (this *Paper) Replace() error {
	return this.GetFactory().Replace(this)
}

func (this *Paper) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *Paper) Delete() error {
	return this.GetFactory().Delete(this)
}

func (this *Paper) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *Paper) Destroy() error {
	return this.GetFactory().Destroy(this)
}

func (this *Paper) ToString() string {
	return string(this.ToBytes())
}

func (this *Paper) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *Paper) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *Paper) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}

func (this *Paper) LoadFlow() {
	if this.FlowId == 0 {
		return
	}
	this.Flow, _ = NewFlowCrud().FindId(this.FlowId)
}

func (this *Paper) LoadProcess() {
	if this.ProcessId == 0 {
		return
	}
	this.Process, _ = NewProcessCrud().FindId(this.ProcessId)
}

func (this *Paper) LoadEmp() {
	if this.EmpId == 0 {
		return
	}
	this.Emp, _ = NewEmpCrud().FindId(this.EmpId)
}

func (this *Paper) LoadSrcPapers() {
	srcPapers, _ := NewPaperLinksCrud().FindBy(ss.M{"destPaperId": this.Id}, []string{"id"}, 0, 0)
	this.SrcPapers = srcPapers
}

func (this *Paper) LoadUsedPapers() {
	usedPapers, _ := NewPaperLinksCrud().FindBy(ss.M{"srcPaperId": this.Id}, []string{"id"}, 0, 0)
	this.UsedPapers = usedPapers
}

func (this *Paper) ClearRelationsBeforeSave() mgo.Doc {

	this.Flow = nil

	this.Process = nil

	this.Emp = nil

	this.SrcPapers = nil

	this.UsedPapers = nil

	return this
}

func neverUsed_Paper() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type PaperAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *Paper
	Session *ez.Session
}

func NewPaperAccessControl(model *Paper, action string, session *ez.Session) *PaperAccessControl {
	ctrl := &PaperAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.PaperAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
